<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* 	Class Name	: member
	Author 		: Iswan Putera S.Kom
	Version		: 2.0
	Date		: 09 May 2013
*/

class Member extends CI_Controller
{
	function __construct()
	{
		parent::__construct();	
		$this->load->model('main_model');
		$this->load->library('zetro_auth');
		$this->load->library('zetro_func');
		//force_ssl();
	}
//registration
	function newdata()
	{
		$data=array();
		//menu header
		$data['tab_header']=$this->zetro_form->generate_tab_header('Add New Member');
		//generate form basic data
		$data['id_reg']=empty($_GET['id_reg'])?'':$_GET['id_reg'];
		$this->zetro_form->frm_file('asset/frm/zetro_member.frm');
		$this->zetro_form->section('newmember');
		$this->zetro_auth->menus('add_new_member');
		$this->zetro_form->generate_dropdown_content('lokasi','id_lokasi','nm_lokasi','','id_lokasi');
	    $this->zetro_form->buttons(true,'Save + Next');
		$data['form']=$this->zetro_form->frm_std();
		//generat personal form
		$this->zetro_form->section('memberdetail');
		$this->zetro_auth->menus('add_new_member');
		$this->zetro_form->buttons(true,'Saved');
		$data['form2']=$this->zetro_form->frm_std('frm2');
		
		//send data to view
		$this->load->view('admin/header');
		$this->load->view('keanggotaan/new_register',$data);
		$this->load->view('admin/footer');
	}
	//generate registratsi member

	function get_no_registration()
	{
		//generate nomor registrasi komposisi [yyyymmdd-squence number]
		//squence number akan kembali ke awal di setiap hari
		$data=array();$num='';
		$this->main_model->table_name('v_no_reg');
		$this->main_model->orderby('created_date');
		$this->main_model->set_methods('select_ordered');
		$this->main_model->query();
		$this->main_model->where(array('tgl'=>date('Ymd')));
		$data=$this->main_model->set_data();
		foreach ($data as $r)
		{
			$num=$r->nomor;
		}
		echo ($num=='')?date('Ymd').'-0001':date('Ymd').'-'.repeater('0',(4-strlen((int)$num))).($num+1);
	}
	function set_register()
	{
		//simpan data inputan ke table member mode replace
		$data=array();
		$data['id_reg']		=empty($_POST['id_reg'])?''		:$_POST['id_reg'];
		$data['id_lokasi']	 =empty($_POST['id_lokasi'])?''	 :$_POST['id_lokasi'];
		$data['nm_member']	 =empty($_POST['nm_member'])?'' 	 :$_POST['nm_member'];
		$data['nip_member']	=empty($_POST['nip_member'])?''	:$_POST['nip_member'];		
		$data['sex_member']	=empty($_POST['sex_member'])?'L'   :$_POST['sex_member'];
		$data['add_member']	=empty($_POST['add_member'])?''	:$_POST['add_member'];
		$data['hp_member']	 =empty($_POST['hp_member'])?''     :$_POST['hp_member'];
		$data['email_member']  =empty($_POST['email_member'])?''  :$_POST['email_member'];
		$data['bop_member']	=empty($_POST['bop_member'])?''	:$_POST['bop_member'];
		$data['bod_member']	=empty($_POST['bod_member'])?'0000-00-00':tglToSql($_POST['bod_member']);
		$data['app_level']	 =empty($_POST['app_level'])?'0'    :$_POST['app_level'];
		$data['app_stat']	  =empty($_POST['app_stat'])?'N'	 :$_POST['app_stat'];
		$data['created_by']    =$this->session->userdata('userid');
		$this->main_model->prepared_data('member',$data);
		echo success($this->main_model->set_data());

	}
	//list member
	function reg_list()
	{
		//menu header
		$data=array();
		$data['id_reg']=empty($_GET['id_reg'])?'':$_GET['id_reg'];
		$data['tab_header']=$this->zetro_form->generate_tab_header('List Register');
		$this->zetro_form->frm_file('asset/frm/zetro_member.frm');
		$this->zetro_auth->menus('list_register');
		$content='';
		$this->zetro_form->section('listregister');
		$this->zetro_form->action_col(true);
		$this->zetro_form->tabel_content($content);
		$data['list']=$this->zetro_form->list_std();
		$data['modal']=$this->zetro_form->dialog('','Detail Registered');
		//send data to view
		$noti['notifikasi']=$this->get_data_fromweb(); 
		$this->load->view('admin/header',$noti);
		$this->load->view('keanggotaan/data_register',$data);
		$this->load->view('admin/footer');

	}
	function get_data_fromweb()
	{
		$data="";
		return $data;
	}
	function get_detail_register()
	{
		//get detail data jabatan for update data
		$data=array();
		$id=empty($_POST['id'])?'':$_POST['id'];
		$this->main_model->table_name('v_member_reg');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$this->main_model->where(array('id_reg'=>$id));
		$data=$this->main_model->set_data();
		
		echo ($data)?json_encode($data[0]):json_encode("{'id_reg':''}");	
	}
	
	function get_list_register()
	{
		/*show list data jabatan
		 1. icon aksi akan di tampilkan sesuai dengan user yang aktif dan status dari id_reg tersebut.
		 2. jika user akti dalam group yang punya otorisasi edit dan status register tsb tidak C ( unupproved)
		    maka buton akan muncul 3 jenis [ process/view,edit,deleted]
		 3. jika user aktif masuk dalam daftar approver dan posisinya harus melakukan approved makan akan muncul
		    button process [cog-icon] dan sebaliknya akan muncul button veiw [zoom icon]
		 4. proses pencarian berdasarkan nama register yang memiliki phrase yang dikirim dari variable cari
		    dan data yang di tampilkan dari semua status 
		*/
		$content='';$i=0;$lst_usr=array();
		$ordby=empty($_POST['ordby'])?'id_reg':$_POST['ordby'];
		$this->main_model->table_name('v_member_reg');
		$this->main_model->query();
		if(empty($_POST['cari']))
		{
			(empty($_POST['sa']) || $_POST['sa']=='no')?
			$this->main_model->where_in('app_stat',array('C','N')):
			$this->main_model->where_in('app_stat',array('D'));
		}else{
			$this->main_model->where_like('nm_member',$_POST['cari'],'after');
		}
			
		$this->main_model->orderby($ordby);
		$this->main_model->set_methods('select_ordered');
		$lst_usr=$this->main_model->set_data();
		$this->zetro_auth->menus('list_register');
		$this->zetro_func->menus('list_register');
		foreach($lst_usr as $r)
		{
			$i++;
			$this->zetro_func->reg_number($r->id_reg);
			$icon=($this->zetro_auth->cek_oto('e')!='' && $r->app_stat!='D')?
				  $this->zetro_form->z_icon($r->id_reg,array($this->zetro_func->set_approval(),'cus-page-white-edit','cus-cross'),array($this->zetro_func->set_approval('aksi'),'edit','deleted')):
				  $this->zetro_form->z_icon($r->id_reg,array('cus-zoom','cus-control-stop','cus-control-stop'),array('detail','',''));
			$content.=tr().td($i,false,'','','center').
					  td($r->id_reg).td($r->nm_lokasi).
					  td($r->nm_member).td($r->sex_member,false,'','','center').
					  td($r->add_member).td($r->hp_member).
					  td($r->email_member).
					  td($icon,false,'','','center')._tr();
		}
			$content.=$this->zetro_form->DataNotFound($lst_usr,4);
			echo $content;
			
	}
	function update_approval()
	{
		/*
		1. update status register level by level
		2. jika yang melakukan update di level terakhir dan di approve maka register tersebut akan
		   masuk ke daftar anggota baru dan akan aktif jika sudah melakukan pembayaran iruan yang telah di tentukan
		   dan di table member status di rubah ke R ( registered);
		3. variable approper untuk mendapatkan jumlah level approver
		*/
		//$email="";
		$id	 =$_POST['id'];
		$lvl	=$_POST['lvl'];
		$rsn	=empty($_POST['rsn'])?'':$_POST['rsn'];
		$approper=$this->main_model->rdb('v_app_doc','app_level',"where id_doc='list_register'");
		$email=$this->main_model->rdb('users','useremail',"where userid='".
			   $this->main_model->rdb('v_app_doc',"level_".$lvl,"where id_doc='list_register'")."'");
		$this->main_model->table_name('member');
		$this->main_model->set_methods('update');
		($lvl==$approper)?
		$this->main_model->get_content(array('app_level'=>$lvl,'app_stat'=>'R','created_by'=>$this->session->userdata('userid'))):
		$this->main_model->get_content(array('app_level'=>$lvl,'app_stat'=>'C','created_by'=>$this->session->userdata('userid')));
		$this->main_model->where(array('id_reg'=>$id));
		$message="Mohon di approved Calon Anggota baru berikut:\r\n";
		$message.="Nama Register  :".$this->main_model->rdb("member","nm_member","where id_reg='".$id."'")."\r\n";
		$message.="NIP			:".$this->main_model->rdb("member","nip_member","where id_reg='".$id."'")."\r\n";
		$message.="Cabang			:".$this->main_model->rdb("v_member_reg","nm_lokasi","where id_reg='".$id."'")."\r\n";
		$message.="Mohon login di Ziebar applikasi untuk approved atau un appoved data tersebut.\r\n";
		echo success($this->main_model->set_data());
		($lvl<=$approper)?$this->send_email($email,$message):'';
	}
	function deleted_register()
	{
		//merubah status member menjadi C (close) masukan alasan tidak di setujui
		$id	 =$_POST['id'];
		$lvl	=empty($_POST['lvl'])?$this->main_model->rdb('v_member_reg','app_level',"where id_reg='".$id."'"):$_POST['lvl'];
		$rsn	=empty($_POST['rsn'])?'':$_POST['rsn'];
		//update table member
		$this->main_model->table_name('member');
		$this->main_model->set_methods('update');
		$this->main_model->get_content(array('app_stat'=>'D','created_by'=>$this->session->userdata('userid')));
		$this->main_model->where(array('id_reg'=>$id));
		$this->main_model->set_data();	
		//update reason for table member_reg
		$this->main_model->table_name('member_reg');
		$this->main_model->set_methods('update');
		$this->main_model->get_content(array('app_level'=>$lvl,'app_reason'=>$rsn));
		$this->main_model->where(array('id_reg'=>$id));
		$this->main_model->set_data();	
	}
	
	function detail_registered()
	{
		//tampilkan data detail untuk di edit atau hanya view saja
		$data=array();$lst_usr=array();$fr='';
		$id=$_POST['id'];
		$fr=empty($_POST['fr'])?'':$_POST['fr'];
		$this->main_model->table_name('v_member_reg');
		$this->main_model->query();
		$this->main_model->where(array('id_reg'=>$id));
		$this->main_model->set_methods('select');
		$lst_usr=$this->main_model->set_data();
		//print_r($lst_usr[0]);
		($fr=='')?
		$this->load->view('keanggotaan/detail_registered',$lst_usr[0]):
		$this->load->view('keanggotaan/detail_new_member',$lst_usr[0]);
	}
	
	function detail_registerd_list()
	{
		/*
		1.tampilkan data status register member sudah sampai dimana approval nya
		2.jika user aktif dan masuk ke dalam daftar approver document dan level approvalnya untuk user tersebut
		  maka akan muncul button approved dan un aproved
		3.namun jika user yang aktif tidak masuk ke dalam daftar approver maka  akan muncul wait approver dari user
		  yang berhak approved pada level tersebut
		*/
		$data='';$lst_usr=array();$n=0;$approper=0;
		$id=$_POST['id'];
		$stat=empty($_POST['s'])?'v':$_POST['s'];
		$this->main_model->table_name('member_reg');
		$this->main_model->query();
		$this->main_model->where(array('id_reg'=>$id));
		$this->main_model->set_methods('select');
		$app_stat=$this->main_model->rdb('v_member_reg','app_stat',"where id_reg='".$id."'");
		$lst_usr=$this->main_model->set_data();
		foreach($lst_usr as $r)
		{
			if($r->app_level >0)
			{
				$n++;
				echo tr().td('Approved '.$n.' by').td($r->app_by)._tr().
					 tr().td('Approval Date').td(tglFromSql($r->app_date,true))._tr();
			}
		}
    	$data.="<button class='btn btn-small btn-primary' id='approved' onClick=\"btn_click('".$id.":".($n+1)."','appr')\"><i class='cus-cog'></i> Approved</button>\n";
    	$data.="<button class='btn btn-small' id='cancel' onClick=\"btn_click('".$id.":".($n+1)."','noappr')\"><i class='cus-cog'></i> Un Approved</button>\n";
		$approper=$this->main_model->rdb('v_app_doc','app_level',"where id_doc='list_register'");
		$n_app=(($n+1)<= $approper)?$this->main_model->rdb('v_app_doc','level_'.($n+1),"where id_doc='list_register'"):'';
		$det=($app_stat=='D')?
			 "<mark>".$this->main_model->rdb('v_member_reg','app_reason',"where id_reg='".$id."'")."</mark><br> on ".
			 tglFromSql($this->main_model->rdb('v_member_reg','app_date',"where id_reg='".$id."'"),true):
			 "<mark>Wait for approval</mark>";
		if($n < $approper)
		{
			echo tr().td(($app_stat=='D')?'Not Approved with reason':'Approved '.($n+1)).
				td(($this->session->userdata('userid')==$n_app && $stat!='v')?$data:$det)._tr().
				tr().td(($app_stat=='D')?'Deleted by':'Approval must do by').
				td($this->main_model->rdb('v_users','username',"where userid='".$n_app."'"))._tr();
			
		}
			
	}
	function send_email($email='',$keterangan='')
	{
    $config = Array(
	  'protocol' => 'smtp',
	  'smtp_host' => 'ssl://smtp.googlemail.com',
	  'smtp_port' => 465,
	  'smtp_user' => 'contact.ziebar@gmail.com ', // change it to yours
	  'smtp_pass' => 'qweryxvgsty', // change it to yours
	  'mailtype' => 'html',
	  'charset' => 'iso-8859-1',
	  'wordwrap' => TRUE
	);

      $message = $keterangan;
      $this->load->library('email', $config);
      $this->email->set_newline("\r\n");
      $this->email->from('contact.ziebar@gmail.com '); // change it to yours
      $this->email->to($email);// change it to yours
      $this->email->subject('Approval Anggota baru');
      $this->email->message($message);
      if($this->email->send())
		 {
		  echo 'Email sent.';
		 } else{
		  show_error($this->email->print_debugger());
		}
	}
//end of member class
}